﻿/*****************************
 * Author: YanNI
 * Create：2017/7/6 9:13:20
 * Email:  admin@ircove.com
 * QQ:     401608886
 * TEl: 13520446271
 ****************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace VF.Ip
{
    public class IpHelper
    {
        /// <summary>
        /// ip转为bytes[]
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="ip"></param>
        public static void IpTobytes(ref byte[] bytes, string ip)
        {
            var s = ip.Split('.');
            for (int i = 0; i < 4; i++)
            {
                bytes[i] = Convert.ToByte(s[i]);
            }
        }

        internal static Root root;

        internal static object objlock = new object();

        internal static Root Intance()
        {
            if (root != null)
            {
                return root;
            }

            lock (objlock)
            {
                if (root == null)
                {
                    root = Load();
                }
            }
            return root;
        }


        internal static Root Load()
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["ipbin"]);
            Address.Load();
            Root root = new Root(0);
            byte[] startbs;
            //byte[] endbs;
            for (int i = 0; i < 256; i++)
            {
                root.child.Add(new Node((byte)i));
            }
            int postion = 0;
            Node cur = root;
            using (var bw = new BinaryReader(File.OpenRead(path)))
            {
                int count = bw.ReadInt32();
                for (int i = 0; i < count; i++)
                {

                    startbs = bw.ReadBytes(4);
                    postion = bw.ReadInt32();
                    cur = root.Add(startbs);
                    cur.pos = postion;
                }
            }
            return root;
        }

        public static Address Search(string ip)
        {
            byte[] startbs = new byte[4];
            IpTobytes(ref startbs, ip);
            try
            {
                Address adds = Intance().Search(startbs);
                return adds;
            }
            catch 
            {
                return new Address();
            }
        }

        //internal static void Create()
        //{
        //    Address.Load();
        //    var lines = File.ReadLines(@"E:\ip.merge.txt");
        //    byte[] startbs = new byte[4];
        //    using (var bw = new BinaryWriter(File.Create(@"e:\ip.bin")))
        //    {
        //        int count = lines.Count();
        //        Console.WriteLine("count:" + count);
        //        bw.Write(count);
        //        foreach (var line in lines)
        //        {
        //            var items = line.Split('|');
        //            int p = Address.GetPostion(items[2], items[4], items[5]);
        //            var startip = items[0];
        //            IpHelper.IpTobytes(ref startbs, startip);
        //            bw.Write(startbs);
        //            bw.Write(p);
        //        }
        //        bw.Flush();
        //    }
        //}
    }
}
